package com.jeesite.modules.kj.api.controller.activity;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.enumCode.UnicomResponseEnums;
import com.jeesite.modules.kj.api.vo.DataListPageVo;
import com.jeesite.modules.kj.api.vo.request.ActivityListRequestVo;
import com.jeesite.modules.kj.api.vo.response.ActivityDetailResponseVo;
import com.jeesite.modules.kj.api.vo.response.ActivityResponseVo;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import com.jeesite.modules.kj.api.wechatUtils.TokenUtils;
import com.jeesite.modules.kj.entity.KjActivity;
import com.jeesite.modules.kj.entity.MemberActivity;
import com.jeesite.modules.kj.entity.MemberContributeProject;
import com.jeesite.modules.kj.service.KjActivityService;
import com.jeesite.modules.kj.service.KjMemberJobService;
import com.jeesite.modules.kj.utils.LikesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:
 */
@Api(value = "活动api", tags = {"活动API"})
@Slf4j
@Controller
@RequestMapping(value = "/api/activity")
public class ActivityApiController {
    private final static Logger logger = LoggerFactory.getLogger(ActivityApiController.class);
    @Autowired
    KjActivityService kjActivityService;
    @Autowired
    KjMemberJobService kjMemberJobService;

    @ApiOperation(value = "获取活动列表", notes = "获取活动列表")
    @GetMapping(value = "list")
    public ResponseBean<List<ActivityResponseVo>> list(ActivityListRequestVo vo) {
        List<KjActivity> activityList = kjActivityService.findList(vo.convertToEntity());
        List<ActivityResponseVo> vos = ListUtils.newArrayList();
        for (KjActivity kjActivity: activityList
        ) {
            vos.add(new ActivityResponseVo(kjActivity));
        }
        return new ResponseBean<>(vos);
    }

    @ApiOperation(value = "获取热门活动", notes = "获取热门活动")
    @GetMapping(value = "hot")
    public ResponseBean<List<ActivityResponseVo>> hot() {
        KjActivity where = new KjActivity();
        where.setActivityStatus(KjActivity.SIGN_START_STATUS);
        where.setIsHot(KjActivity.ISHOT);
        //审核通过
        where.setActivityAudit("1");
        where.getSqlMap().getOrder().setOrderBy("a.isort desc");
        List<KjActivity> activityList = kjActivityService.findList(where);
        List<ActivityResponseVo> vos = ListUtils.newArrayList();
        for (KjActivity kjActivity: activityList
        ) {
            vos.add(new ActivityResponseVo(kjActivity));
        }
        return new ResponseBean<>(vos);
    }

    @ApiOperation(value = "获取活动详情", notes = "获取活动详情")
    @GetMapping(value = "{id}")
    public ResponseBean<ActivityDetailResponseVo> detail(@PathVariable(name = "id") String id, HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("Authorization");
        KjActivity kjActivity = kjActivityService.get(id);
        if(KjActivity.STATUS_NORMAL.equals(kjActivity.getStatus())){
            ActivityDetailResponseVo result = new ActivityDetailResponseVo(kjActivity);

            if(StringUtils.isNotBlank(token)) {
                String memberId = TokenUtils.verify(token);
                result.setIsLike(LikesUtil.getIsLike(id, memberId));
            }else{
                result.setIsLike("0");
            }
            return new ResponseBean<ActivityDetailResponseVo>(result);
        }else{
            return new ResponseBean<ActivityDetailResponseVo>("false",new ActivityDetailResponseVo(), UnicomResponseEnums.FAIL_GETDATA.getCode(),"该活动已被删除！");
        }
    }



    @ApiOperation(value = "我参与的活动", notes = "我参与的活动")
    @PostMapping(value = "my")
    @UserLoginToken
    public ResponseBean<DataListPageVo> my(@ApiIgnore @CurrentUser String memberId, String year, String month, int pageNo, int pageSize) {
        Page<MemberActivity> page = new Page<>(pageNo, pageSize);
        page.setList(kjMemberJobService.findMemberActivies(memberId, year, month, pageNo, pageSize));
        DataListPageVo vos = new DataListPageVo<>();
        vos.setCount(page.getCount());
        vos.setList(page.getList());
        vos.setPageNo(page.getPageNo());
        vos.setPageSize(page.getPageSize());
        return new ResponseBean<>(vos);
    }

}